package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author hljstart
 * @create 2022-05-25-12:01
 */
@Controller
public class IndexController {

    @Reference
    private AdminService adminService;

    @Reference
    private PermissionService permissionService;
    /*//去首页
    @RequestMapping("/")
    public String index(){
        return "frame/index";
    }*/
    //去首页
    @RequestMapping("/")
    public String index(Map map){
        //设置默认的用户id
        //Long userId=1L;
        //调用adminService中查询的方法
        //Admin admin = adminService.getById(userId);
        //通过SpringSecurity获取user对象
        User user= (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //调用AdminService中根据用户名获取Admin对象的方法
        Admin admin = adminService.getAdminByUserName(user.getUsername());
        //根据用户的id调用PermissionService中获取用户权限菜单的方法
        List<Permission> permissionList=permissionService.getMenuPermissionsByAdminId(admin.getId());
        //将用户和用户权限菜单放到request域中
        map.put("admin",admin);
        map.put("permissionList",permissionList);
        return "frame/index";
    }
    @RequestMapping("/main")
    public String main(){
        return "frame/main";
    }

    //去登录页面
    @RequestMapping("/login")
    public String login(){
        return "frame/login.html";
    }

    //去没有权限的提示页面
    @RequestMapping("/auth")
    public String auth(){
        return "frame/auth";
    }
}
